package com.hc.util;

import java.util.Calendar;
import java.util.Date;
public class DateUtilExt {
	
	public static Date getFirstMiniteOfHour(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		
		return calendar.getTime();
	}
	
	public static Date getLastMiniteOfHour(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.HOUR_OF_DAY, 1);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		
		return calendar.getTime();
	}
	
	public static Date getFirstTimeOfDay(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		
		return calendar.getTime();
	}
	
	public static Date getLastTimeOfDay(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		
		if(calendar.getTime().getTime()>=System.currentTimeMillis()){
			
			return new Date();
		}else{
			
			return calendar.getTime();
		}
		
	}
	
	public static Date getFirstDayOfWeek(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, 2-calendar.get(Calendar.DAY_OF_WEEK));
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		
		return calendar.getTime();
	}
	
	public static Date getLastDayOfWeek(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, 8 - calendar.get(Calendar.DAY_OF_WEEK));
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		
	if(calendar.getTime().getTime()>=System.currentTimeMillis()){
			
			return new Date();
		}else{
			
			return calendar.getTime();
		}
	}
	
	public static Date getFirstDayOfMoth(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		
		return calendar.getTime();
	}
	
	public static Date getLastDayOfMoth(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		
	if(calendar.getTime().getTime()>=System.currentTimeMillis()){
			
			return new Date();
		}else{
			
			return calendar.getTime();
		}
	}
	
	public static void main(String[] args) {
		
		Calendar calendar = DateUtil.str2Calendar("2012-02-06 00:00:00", DateUtil.DEFAULT_STRING);
		System.out.println(DateUtil.date2Str(getFirstDayOfMoth(calendar.getTime()), DateUtil.DEFAULT_STRING));
		System.out.println(DateUtil.date2Str(getLastDayOfMoth(calendar.getTime()), DateUtil.DEFAULT_STRING));
//		
		System.out.println(DateUtil.date2Str(getLastDayOfWeek(calendar.getTime()), DateUtil.DEFAULT_STRING));
		System.out.println(DateUtil.date2Str(getFirstDayOfWeek(calendar.getTime()), DateUtil.DEFAULT_STRING));
		
		
		System.out.println();
		System.out.println();
		
		System.out.println(DateUtil.date2Str(getLastMiniteOfHour(calendar.getTime()), DateUtil.DEFAULT_STRING));
		System.out.println(DateUtil.date2Str(getFirstMiniteOfHour(calendar.getTime()), DateUtil.DEFAULT_STRING));
	}
}
